<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/lib/jq-module/zyupload/zyupload-1.0.0.min.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>

<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
            </div>
        </script>

        <table class="layui-hide" id="counelTable" lay-filter="currentTableFilter"></table>

    </div>
</div>

<script type="text/html" id="barDemo">
    <input type="button" class="layui-btn layui-btn-xs" lay-event="edit" value="下载">
    <input type="button" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" value="删除">
</script>
</body>
<script src="/lib/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['layedit', 'form', 'table', 'jquery',], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layedit = layui.layedit;


        table.render({
            elem: '#counelTable'
            , id: 'counelTable'
            // , height: 300
            // , width: 800
            , url: "/info/findAll.do" //数据接口
            , page: true //开启分页
            , type: "get"
            , toolbar: "#toolbarDemo"
            , request: {
                pageName: 'pageNow' //页码的参数名称，默认：page
                ,
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            // , toolbar: '#toolbarDemo'
            , limit: 2
            , limits: [1, 2, 3, 4, 5]
            , method: 'get'

            , cols: [[ //表头
                {type: 'checkbox'}
                , {field: 'title', title: '文件标题', align: 'center'}
                , {field: 'id', title: 'ID',  align: 'center', hide: true}
                , {field: 'fileName', title: 'fileName', align: 'center', hide: true}
                , {field: 'content', title: '下载内容', align: 'center'}
                , {field: 'state', title: '状态', align: 'center', templet: "#typeTpl"}
                , {field: 'publisher', title: '发布人', align: 'center'}
                , {field: 'publishTime', title: '上传时间', align: 'center',templet : "<div>{{layui.util.toDateString(d.publishTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"}
                , {title: '操作', align: 'center', toolbar: '#barDemo'}

            ]]
            , done: function (res, curr, count) {
                var that = this.elem.next();
                res.data.forEach(function (item, index) {

                })
            }
        });

        /**
         * 1.删除公告
         * 2.修改公告
         * **/
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {

                layer.confirm('确定删除该角色吗？', {
                    btn: ['确定', '取消'] //可以无限个按钮
                }, function (index, layero) {
                    $.ajax({
                        url: "/info/remove.do"
                        , dataType: "json"
                        , type: "get"
                        , data: {"id": data.id}
                        , success: function (rec) {
                            if (rec.code == 0) {
                                layer.msg("恭喜删除成功");
                                setTimeout(function () {
                                    location.href = "/html/notice/counsel.html"
                                }, 2000)
                            } else {
                                layer.msg("抱歉删除失败");
                                setTimeout(function () {
                                    location.href = "/html/notice/counsel.html"
                                }, 2000)
                            }
                        }
                    })
                }, function (index) {

                });

            } else if (obj.event === 'edit') {
                window.location.href = "/info/down.do?filename=" + data.fileName;
            }
        });

        /**
         * 发送
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            var demo = null;

            /*  layedit.build('demo'); //建立编辑器*/
            if (obj.event === 'add') {// 监听添加操作
                var index = layer.open({
                    type: 1
                    , title: "发送文件" //不显示标题栏
                    , closeBtn: false
                    , area: ['80%', '80%']
                    , shade: 0.8
                    , id: 'LAY_layuipro' //设定一个id，防止重复弹出
                    , btn: ['发送', '取消']
                    , btnAlign: 'c'
                    , moveType: 1 //拖拽模式，0或者1
                    , content: '<from class="layui-form layuimini-form">' +
                        '<from class="layui-form layuimini-form">\n' +
                        '    <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label required">文件标题</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '        <input type="text" name="title" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入文件标题" value="" class="layui-input">\n' +
                        '    </div>\n' +
                        '</div>\n' +
                        '    <div class="layui-form-item">\n' +
                        '        <label class="layui-form-label required">上传文件</label>\n' +
                        '        <div class="layui-input-block">\n' +
                        '            <input type="file" name="file" lay-verify="required" lay-reqtext="不能为空" placeholder="请输入上传文件" value="" class="layui-input">\n' +
                        '        </div>\n' +
                        '    </div>\n' +
                        '    <div class="layui-form-item">\n' +
                        '        <label class="layui-form-label required">发布人</label>\n' +
                        '        <div class="layui-input-block">\n' +
                        '            <input type="publisher" name="publisher" lay-verify="required" lay-reqtext="不能为空" placeholder="发布人" value="" class="layui-input">\n' +
                        '        </div>\n' +
                        '    </div>\n' +
                        '    <div class="layui-form-item">\n' +
                        '        <label class="layui-form-label required">上传时间</label>\n' +
                        '        <div class="layui-input-block">\n' +
                        '           <input type="date" name="uploadtime" lay-verify="data" autocomplete="off" class="layui-input" lay-reqtext="不能为空" placeholder="时间">\n' +
                        '        </div>\n' +
                        '    </div>\n' +
                        '    <div class="layui-form-item">\n' +
                        '        <label class="layui-form-label required">上传内容</label>\n' +
                        '        <div class="layui-input-block">\n' +
                        '            <textarea id="demo" style="display: none;" name="demo"></textarea>\n' +
                        '        </div>\n' +
                        '    </div>\n' +
                        '</from>'
                    , success: function (layero, index) {
                        demo = layedit.build('demo'); //建立编辑器
                        form.render('radio');

                    }, yes: function (index, layero) {
                        var title = $("input[name=title]").val();
                        var uploadTime = $("input[name=uploadtime]").val();
                        var content = layedit.getContent(demo);
                        var state = $("input[name=state]").val();
                        var publisher = $("input[name=publisher]").val();
                        var formData = new FormData();
                        var file = $("input[name='file']").val();

                        formData.append("title",title);
                        formData.append("uploadTime",uploadTime);
                        formData.append("content",content);
                        formData.append("publisher",publisher);
                        formData.append("file",$("input[name='file']")[0].files[0])


                        if (title == null || title == "") {
                            layer.msg("文件标题不能为空")
                        } else if (uploadTime == null || uploadTime == "") {
                            layer.msg("发布时间不能为空")
                        } else if (content == null || content == "") {
                            layer.msg("发布内容不能为空")
                        } else {
                            $.ajax({
                                url: "/info/add.do"
                                , dataType: "json"
                                , type: "post"
                                ,contentType:false
                                ,processData: false
                                , data: formData
                                , success: function (rec) {
                                    if (rec.code == 0) {
                                        layer.msg("发送成功...");
                                        setTimeout(function () {
                                            location.href = "/html/notice/notice.html"
                                        }, 2000)
                                    } else {
                                        layer.msg("发送失败");
                                        setTimeout(function () {
                                            location.href = "/html/notice/notice.html"
                                        }, 2000)
                                    }
                                }

                            })

                        }

                    }
                })

            }
        });

    });
</script>
<!--<script type="text/html" id="typeTpl">-->
<!--    {{#  if(d.state ==0 ){ }}-->
<!--    <p>未发布</p>-->
<!--    {{#  } else if(d.state ==1 ){ }}-->
<!--    <p>已发布</p>-->
<!--    {{#  } }}-->
<!--</script>-->
</html>